/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.io.Hex;
import wtkx.io.UrlcInputStream;
import wtkx.ui.Cursor;
import wtkx.ui.Insets;
import wtkx.ui.Keyboard;
import wtkx.ui.Mouse;

import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/**
 *
 * @author gbrown
 * @author jdp
 */
public class Image
    extends Component
    implements java.awt.image.ImageObserver
{


    protected volatile URL src;

    protected volatile BufferedImage img;


    public Image() {
        super();
    }
    public Image(URL src) {
        this();
        this.setSrc(src);
    }


    public final URL getSrc() {
        return src;
    }
    public final void setSrc(URL src) {
        if (null != src)
            this.src = src;
        else
            throw new IllegalArgumentException();
    }
    public final void setSrc(String string){
        URL url = this.getInputURL(string);
        this.setSrc(url);
    }
    protected BufferedImage getImage(){
        if (null == this.img)
            this.reload(true);
        return this.img;
    }
    public boolean reload(boolean force){
        URL src = this.src;
        if (null != src){
            if (force || null == this.img){
                this.img = this.getResourceCache().getImage(this,src);
            }
        }
        return false;
    }
    public void layout(){
        BufferedImage img = this.getImage();
        if (null != img){
            float w = img.getWidth();
            float h = img.getHeight();
            if (0f < w && 0f < h){
                this.setInnerSize(w,h);
                super.layout();
            }
            else {
                this.invalidate();
            }
        }
    }
    public void paint(Graphics2D g) {
        BufferedImage img = this.getImage();
        if (null != img){
            Insets padding = this.padding;
            int x = (int)padding.left;
            int y = (int)padding.top;
            g.drawImage(img,x,y,this);
        }
    }
    public boolean equals(Image that){
        if (this == that)
            return true;
        else if (that == null)
            return false;
        else if (null != this.src && null != that.src)
            return this.src.equals(that.src);
        else
            return false;
    }
    public boolean imageUpdate(java.awt.Image img, int flags,
                               int x, int y, int width, int height)
    {
        boolean repaint;
        if ((flags & (FRAMEBITS|ALLBITS)) != 0)
            repaint = false;
        else if ((flags & SOMEBITS) != 0)
            repaint = true;
        else
            repaint = false;

        if (repaint)
            this.repaint();

        return (0 == (flags & (ALLBITS|ABORT)));
    }
    public void endElement(){
        this.reload(false);
    }
}
